import wepy from 'wepy'
export default class extends wepy.mixin {
    data = {
        // 商品的Id值
        goods_id: '',
        // 商品的详情
        goodsInfo: {},
        addressInfo: null
    }
    computed = {
        addressStr() {
            if (this.addressInfo === null) {
                return '请选择收货地址'
            }
            const addr = this.addressInfo
            const str =
                addr.provinceName + addr.cityName + addr.countyName + addr.detailInfo
            return str
        },
        total() {
            return this.$parent.globalData.total
        }
    }
    methods = {
        // 点击预览图片
        preview(current) {
            wepy.previewImage({
                // 所有图片的路径
                urls: this.goodsInfo.pics.map(item => item.pics_big),
                // 当前默认看到的图片
                current: current
            })
        },
        // 获取用户的收货地址
        async chooseAddress() {
            const res = await wepy.chooseAddress().catch(err => err)

            if (res.errMsg !== 'chooseAddress:ok') {
                return wepy.baseToast('获取收货地址失败！')
            }

            this.addressInfo = res
            // 将选择的收获地址，存储到本地 Storage 中
            wepy.setStorageSync('address', res)
            this.$apply()
        },
        // 给加入购物车按钮绑定点击事件
        addToCart() {
            // console.log(this.$parent.globalData);
            this.$parent.addGoodsToCart(this.goodsInfo)
            wepy.showToast({
                title: '加入购物车成功',
                icon: 'success'
            })
        }
    }
    onLoad(options) {
        // 转存商品Id
        this.goods_id = options.goods_id
        // 发起数据请求
        this.getGoodsInfo()
    }
    // 获取商品详情数据
    async getGoodsInfo() {
        const { data: res } = await wepy.get('/goods/detail', {
            goods_id: this.goods_id
        })

        if (res.meta.status !== 200) {
            return wepy.baseToast()
        }

        this.goodsInfo = res.message
        this.$apply()
    }
}